home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 342_01.zip / I8255F03.C < prev    next >
C/C++ Source or Header  |  1993-04-03  |  4KB  |  141 lines

  1. /*-
  2.  *  ----------------------------------------------------------------------
  3.  *  File        :   I8255F03.C
  4.  *  Creator     :   Blake Miller
  5.  *  Version     :   01.00.00        February 1991
  6.  *  Language    :   Microsoft C     Version 5.1
  7.  *  Purpose     :   Intel 8255 Compatible Digital IO Functions
  8.  *                  Bit Put Function
  9.  *  ----------------------------------------------------------------------
  10.  */
  11.  
  12. #define  I8255F03_C_DEFINED  1
  13. #include "I8255FN.H"
  14. #undef   I8255F03_C_DEFINED
  15.  
  16. void I8255_bitput (I8255DAT *data, int bit, int state);
  17.  
  18. /*- I8255 : Bit Put --------------------------**
  19.  *  Set/Clear one of the bits in the 8255.
  20.  *  A state of 1 sets the bit and a state of 0 clears the bit.
  21.  *  The bit number should be from 1 - 24 as follows:
  22.  *   1 = Port A Bit 0;   8 = Port A Bit 7
  23.  *   9 = Port B Bit 0;  16 = Port B Bit 7
  24.  *  17 = Port C Bit 0;  24 = Port C Bit 7
  25.  *  Passed:
  26.  *      pointer :   I8255DAT
  27.  *      integer :   bit number
  28.  *      integer :   state : TRUE (!0) = SET, FALSE (0) = CLEAR
  29.  *  Returns:
  30.  *      nothing
  31.  *      Loads stat with appropriate error code
  32.  */
  33. void I8255_bitput (I8255DAT *data, int bit, int state)
  34.     {
  35.     int             port;   /* port number  */
  36.     int             padd;   /* port address */
  37.     unsigned char   mask;   /* byte mask    */
  38.     unsigned char   oval;   /* out value    */
  39.  
  40.     /*  Make sure the bit requested is valid.
  41.      *  Three ports of 8 bits each = 24 bits.
  42.      */
  43.     if ( (bit < 1) || (bit > 24) ){
  44.         data->stat = I8255_ST_BB;
  45.         return;
  46.         }
  47.  
  48.     /*  Decrement the bit so we have zero offset.
  49.      *  This is useful for lining up the port with integer
  50.      *  divide and it is also used for left shifting to
  51.      *  create a mask.
  52.      *  Get port number by doing integer division.  The port
  53.      *  number should end up as 0, 1, or 2, corresponding to
  54.      *  PORT A, PORT B, and PORT C respectively.
  55.      *  Then do modulo 8 to get the corrsponding bit
  56.      *  number to be used in the particular byte.  This
  57.      *  value will end up being 0 - 7.
  58.      *  Then form the bit mask, starting with 1 and
  59.      *  left shifting to line it up with the proper bit.
  60.      *  If the bit is to be set, then the mask is bit ored (|)
  61.      *  with the current port value and then output.  If it is
  62.      *  to be cleared, then the mask is inverted and
  63.      *  this vlaue is bit anded (&) with the current port value.
  64.      */
  65.     bit--;
  66.     port = ( bit / 8 );     /* port number (0 - 2)          */
  67.     bit = bit % 8;          /* modulo to get the bit number */
  68.     mask = 0x01;            /* assume low bit of byte       */
  69.     mask = mask << bit;     /* shift left to requested bit  */
  70.  
  71.     /*  Assuming bit is to be set:
  72.      *  Switch to appropriate port and set bit.
  73.      */
  74.     if ( state ){           /* bit set  */
  75.         switch ( port ){
  76.             case 0:         /* port A   */
  77.                 data->adat = data->adat | mask;
  78.                 break;
  79.             case 1:         /* port B   */
  80.                 data->bdat = data->bdat | mask;
  81.                 break;
  82.             case 2:         /* port C   */
  83.                 data->cdat = data->cdat | mask;
  84.                 break;
  85.             }
  86.         }
  87.  
  88.     /*  Assuming bit is to be cleared:
  89.      *  Invert bit mask.
  90.      *  Switch to appropriate port and clear bit.
  91.      */
  92.     else {                  /* bit clear    */
  93.  
  94.         mask = ~mask;
  95.  
  96.         switch ( port ){
  97.             case 0:         /* port A   */
  98.                 data->adat = data->adat & mask;
  99.                 break;
  100.             case 1:         /* port B   */
  101.                 data->bdat = data->bdat & mask;
  102.                 break;
  103.             case 2:         /* port C   */
  104.                 data->cdat = data->cdat & mask;
  105.                 break;
  106.             }
  107.         }
  108.  
  109.     /*  Prepare to output data:
  110.      *  Obtain appropriate port data
  111.      *  Obtain port address.
  112.      */
  113.     switch ( port ){
  114.         case 0:         /* port A   */
  115.             padd = I8255_PORTA (data->base);
  116.             oval = data->adat;
  117.             break;
  118.         case 1:         /* port B   */
  119.             padd = I8255_PORTB (data->base);
  120.             oval = data->bdat;
  121.             break;
  122.         case 2:         /* port C   */
  123.             padd = I8255_PORTC (data->base);
  124.             oval = data->cdat;
  125.             break;
  126.         default:        /* bad port number  */
  127.             data->stat = I8255_ST_BP;
  128.             return;
  129.             break;
  130.         }
  131.  
  132.     chp_portwt ( padd, oval );
  133.     data->stat = I8255_ST_OK;
  134.     }
  135.  
  136. /*-
  137.  *  ----------------------------------------------------------------------
  138.  *  END I8255F03.C Source File
  139.  *  ----------------------------------------------------------------------
  140.  */
  141.